package eclipse.lib.sockets;
import eclipse.*;
import eclipse.client.*;
import eclipse.lib.scripting.*;

import javax.swing.*;
/**
 * The ClientDataHandle class handles all data that is sent to the Client from the server.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class ClientDataHandle
{
	private static final char msg_REQUESTMAX 		= 0;
	private static final char msg_REQUESTNEWACCOUNT	= 1;
	private static final char msg_SENDCHARPOS		= 2;
	private static final char msg_REQUESTLOGIN		= 3;
	/** 
	 *  Processes a new net message sent to the Client.
	 * @param netMsg String representing the message recieved.
	 */
	public static final void process(String netMsg)
	{		
		if(netMsg.charAt(0) == msg_REQUESTMAX)
			recieveMaxData(netMsg);
		else if(netMsg.charAt(0) == msg_REQUESTNEWACCOUNT)
			recieveNewAccountConf(netMsg);
		else if(netMsg.charAt(0) == msg_SENDCHARPOS)
			recieveCharPosition(netMsg);
		else if(netMsg.charAt(0) == msg_REQUESTLOGIN)
			recieveLoginConf(netMsg);
	}
	/**
	 * Message processed if the MaxData was recieved
	 * @param netMsg String representing the message recieved.
	 */
	public static final void recieveMaxData(String netMsg)
	{
		Database.MAX_PLAYERS 	= ByteInterpreter.getIntFromString(netMsg, 1);
		Database.MAX_ITEMS		= ByteInterpreter.getIntFromString(netMsg, 3);
		Database.MAX_MAPS		= ByteInterpreter.getIntFromString(netMsg, 5);
		Database.MAX_NPCS		= ByteInterpreter.getIntFromString(netMsg, 7);
		Database.MAX_SHOPS		= ByteInterpreter.getIntFromString(netMsg, 9);
		Database.MAX_SPELLS		= ByteInterpreter.getIntFromString(netMsg, 11);
		Database.MAX_CLASSES	= ByteInterpreter.getIntFromString(netMsg, 13);
		Database.myID			= ByteInterpreter.getIntFromString(netMsg, 15);
		System.out.println(Database.myID);
		Database.AllDataReceived = true;
		Database.player = new eclipse.lib.player.Player[Database.MAX_PLAYERS];
		for(int i = 0; i < Database.MAX_PLAYERS; i++)			
			Database.player[i] = new eclipse.lib.player.Player();
	}
	/**
	 * Packet recieved after a new Account was created
	 * @param netMsg String representing the message recieved. 
	 */
	public static final void recieveNewAccountConf(String netMsg)
	{
		if(netMsg.charAt(1) == 't')
			JOptionPane.showMessageDialog(null, "Account Created!");
		else
			JOptionPane.showMessageDialog(null, "An error occurred while creating your account.");
	}
	/** 
	 * Pakcet sent when requesting a new Account.
	 *
	 */
	public static final void requestNewAccount()
	{
		String msg = msg_REQUESTNEWACCOUNT + "";
		msg = msg + 
			Database.USERNAME + 
			eclipse.Constants.SEP_CHAR + 
			Database.PASSWORD + 
		"\n";
		Database.Socket.sendMsg(msg);
	}
	/**
	 * Packet sent when requesting the max data.
	 *
	 */
	public static final void requestMaxData()
	{
		String msg = msg_REQUESTMAX + "\n";		
		Database.Socket.sendMsg(msg);				
	}		
	/**
	 * Packet sent when sending the character position.
	 *
	 */
	public static final void sendCharPosition()
	{
		String msg = msg_SENDCHARPOS + "";
		final int playerID = Database.myID;
		final int dir 	= Database.player[playerID].character.cDir;
		final int frame	= Database.player[playerID].character.cFrame;
		final float xPos 	= Database.player[playerID].character.cPos.x;
		final float yPos 	= Database.player[playerID].character.cPos.y;
		final float zPos 	= Database.player[playerID].character.cPos.z;
		msg = msg + ByteInterpreter.appendIntegerToString(dir);
		msg = msg + ByteInterpreter.appendIntegerToString(frame);
		msg = msg + ByteInterpreter.appendFloatToString(xPos);
		msg = msg + ByteInterpreter.appendFloatToString(yPos);
		msg = msg + ByteInterpreter.appendFloatToString(zPos);
		msg = msg + "\n";
		Database.Socket.sendMsg(msg);
	}
	/**
	 * Message processed when a character moves.
	 * @param netMsg String representing the message recieved.
	 */
	public static final void recieveCharPosition(String netMsg)
	{
		final int index	= ByteInterpreter.getIntFromString(netMsg, 1);
		final int dir 	= ByteInterpreter.getIntFromString(netMsg, 3);
		final int frame	= ByteInterpreter.getIntFromString(netMsg, 5);
		final float xPos 	= ByteInterpreter.getFloatFromString(netMsg, 7);
		final float yPos 	= ByteInterpreter.getFloatFromString(netMsg, 9);
		final float zPos 	= ByteInterpreter.getFloatFromString(netMsg, 11);	
		if(index == Database.myID)
			return;		
		Database.player[index].character.cDir 	= dir;
		Database.player[index].character.cFrame = frame;
		Database.player[index].character.cPos.x = xPos;
		Database.player[index].character.cPos.y = yPos;
		Database.player[index].character.cPos.z = zPos;
		//System.out.println(xPos);
	}
	public static final void requestLogin()	
	{
		String msg = msg_REQUESTLOGIN + "";
		msg = msg + 
			Database.USERNAME + 
			eclipse.Constants.SEP_CHAR + 
			Database.PASSWORD + 
		"\n";
		Database.Socket.sendMsg(msg);
	}
	public static final void recieveLoginConf(String netMsg)
	{
		if(netMsg.charAt(1) == 't')
		{
			Database.Login.setVisible(false);
			Database.Eclipse.setAlwaysOnTop(true);
			Database.MENUSTATE = Constants.MENUSTATE_CHARSELECT;
			Database.NewCharacter.setVisible(true);
		}
		else
			JOptionPane.showMessageDialog(null, "An error occurred while logging in.");
	}
}